package io.jpress.addon.airworthiness.directive;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jboot.web.directive.base.PaginateDirectiveBase;
import io.jpress.JPressOptions;
import io.jpress.addon.airworthiness.model.JpressAddonAirworthiness;
import io.jpress.addon.airworthiness.service.JpressAddonAirworthinessService;
import io.jpress.commons.directive.DirectveKit;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@JFinalDirective("airworthinessList")
public class AirworthinessDirective extends JbootDirectiveBase {

    @Inject
    private JpressAddonAirworthinessService service;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {

        Controller controller = JbootControllerContext.get();
        controller.setAttr("language",controller.get("language"));
        int page = controller.getParaToInt(1, 1);
        int pageSize = getPara("pageSize", scope,10);

        Columns columns = Columns.create();

        String keyword = controller.getPara("keyword");
        String model = controller.getPara("model");
        if(model==null){
            model = getPara("model", scope);
        }
        String commandNumber = controller.getPara("commandNumber");
        String startTime = controller.getPara("startTime");
        String endTime = controller.getPara("endTime");

        if(keyword!=null && !keyword.equals("")){
            columns.like("title", "%"+keyword+"%").or().like("title_en", "%"+keyword+"%");
        }
        columns.eq("aircraft_model",model).eq("cad_no",commandNumber)
                .ge("effective_date",strtodata(startTime))
                .le("effective_date",strtodata(endTime));

        Page<JpressAddonAirworthiness> entries=service.paginateByColumns(page, pageSize,columns,"effective_date desc");
        scope.setLocal("airworthinessList", entries);
        Record article_meta_info = Db.findById("article_meta_info", 2);
        String value = article_meta_info.getStr("value");
        String[] split = value.split(",");
        scope.setLocal("model",split);
        renderBody(env, scope, writer);

    }

    private Date strtodata(String str){

        if (str==null || str.trim().equals("")) return null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(str);
        } catch (ParseException e) {
            System.out.println("时间格式不正确");
        }

        return time;
    }

    @JFinalDirective("airworthinessPaginate")
    public static class TemplatePaginateDirective extends PaginateDirectiveBase {

        @Override
        protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
            HttpServletRequest request = JbootControllerContext.get().getRequest();
            String url = request.getRequestURI();
            String contextPath = JFinal.me().getContextPath();

            boolean firstGotoIndex = getPara("firstGotoIndex", scope, false);

            if (pageNumber == 1 && firstGotoIndex) {
                return contextPath + "/";
            }

            // 如果当前页面是首页的话
            // 需要改变url的值，因为 上一页或下一页是通过当前的url解析出来的
            if (url.equals(contextPath + "/")) {
                url = contextPath + "/airworthiness/index"
                        + JPressOptions.getAppUrlSuffix();
            }
            return DirectveKit.replacePageNumber(url, pageNumber);
        }

        @Override
        protected Page<?> getPage(Env env, Scope scope, Writer writer) {
            return (Page<?>) scope.get("airworthinessList");
        }

    }

    @Override
    public boolean hasEnd() {
        return true;
    }
}
